package kitrobot.com.bed.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.widget.AdapterView.OnItemClickListener;

import kitrobot.com.bed.Activity.MainActivity;
import kitrobot.com.bed.Activity.PowerdetailActivity;
import kitrobot.com.bed.Activity.VideoActivity;
import kitrobot.com.bed.Service.GetactbedService;
import kitrobot.com.wechat_bottom_navigation.R;

/**
 * on 2017/5/8.
 * 类的描述:
 */

public class CategoryFragment extends Fragment {
    private ListView lv_pwb;
    private List<HashMap<String, Object>> bdata;
    String id=null;
    String token=null;
    private final String img="img";
    private final String bname="bname";
    private final String hlsurl="bhlsurl";
    private SimpleAdapter simAdapter;
    String cname=null;
    String chlsurl=null;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.action, container, false);

        SharedPreferences sp = getActivity().getSharedPreferences("loginToken",
                Context.MODE_PRIVATE);
        id = sp.getString("id", "");
        String phone = sp.getString("phone", "");
        String  name = sp.getString("name", "");
        token = sp.getString("token", "");
        String type=sp.getString("type","");
        lv_pwb= (ListView) view.findViewById(R.id.lv_pwb);


        new Thread(){
            public void run(){
                final String result= GetactbedService.GetactbedByClientPost(id,token);
                if (result!=null){
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            try {
                                //JSON字符串

                                //根据JSON字符串 获得JSON object对象
                                JSONObject jsonObject = new JSONObject(result);
                                //根据key 获取相应的value
                                String errno = jsonObject.getString("errno");
                                String errmsg="年龄:" + jsonObject.getString("errmsg");
                                JSONArray data = jsonObject.getJSONArray("data");


                                bdata = new ArrayList<HashMap<String, Object>>();
                                for (int i = 0; i < data.length(); i++) {
                                    HashMap<String, Object> mapItem = new HashMap<String, Object>();
                                    JSONObject jsonObject2 = (JSONObject) data.get(i);
                                    cname = jsonObject2.getString("name");
                                    chlsurl = jsonObject2.getString("hlsurl");
                                    mapItem.put(bname, cname);
                                    mapItem.put(hlsurl, chlsurl);
                                    mapItem.put(img, R.drawable.icon01);
                                    bdata.add(mapItem);

                                }
                                simAdapter = new SimpleAdapter(
                                        getActivity(), bdata, R.layout.actbedlist,
                                        new String[] { bname,img}, new int[] {
                                        R.id.tv_abna,R.id.img });
                                lv_pwb.setAdapter(simAdapter);

                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }

                    });
                }
                else{
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getActivity().getApplicationContext(),"结果"+result,Toast.LENGTH_LONG).show();
                        }
                    });
                }
            };

        }.start();
        lv_pwb.setOnItemClickListener(new itemClick());

        return view;
    }
    class itemClick implements OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // TODO Auto-generated method stub
            Map<String, String> infoMap = (Map<String, String>) parent.getItemAtPosition(position);
            System.out.println(infoMap.get(hlsurl));
            String pname=infoMap.get("bname");
            System.out.println(infoMap.get("bname"));
            Intent intent = new Intent(getActivity().getApplicationContext(), VideoActivity.class);
            Bundle b = new Bundle();
            b.putString("url", hlsurl);
            intent.putExtras(b);
            getActivity().startActivity(intent);
        }

    }
}
